package com.javaee.controller.doctor;

import com.javaee.BeforeBaseController;
import com.javaee.entity.Doctor;
import com.javaee.entity.Mzgh;
import com.javaee.entity.Record;

import com.javaee.service.doctor.DRecordService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.time.LocalDateTime;
import java.util.List;

@Controller
@RequestMapping("/doctor")
public class DRecordController extends BeforeBaseController {
    @Autowired
    private DRecordService dRecordService;

    @RequestMapping("/showDRecordList")
    public String showDRecordList(HttpSession session,Model model, @RequestParam(defaultValue = "1")Integer currentPage){
        Doctor doctor = (Doctor) session.getAttribute("Doctor");
        int dId = doctor.getId();
        int totalCount =dRecordService.selectCount(dId);
        int pageSize = 5;
        int totalPage = (int)Math.ceil(totalCount*1.0/pageSize);
        List<Record> records=dRecordService.findByDoctorId((currentPage-1)*pageSize, pageSize,dId);
        model.addAttribute("totalPage", totalPage);
        model.addAttribute("currentPage", currentPage);
        model.addAttribute("records", records);
        return "doctor/dShowRecordList";
    }

   @RequestMapping("/showADetail")
   public String showDetail(Model model,int id){
        Record record=dRecordService.findARecordById(id);
        String detail=record.getDetail();
        String htmlDetail = dRecordService.convertMarkdownToHtml(detail);
        model.addAttribute("detail",htmlDetail);
        return "doctor/dShowARecordDetail";
   }
   @RequestMapping("/showAddARecord")
   public String showAddARecord(Model model, @RequestParam int mId){
        Mzgh mzgh=dRecordService.selectByMId(mId);
        String cno=dRecordService.selectCnoByUId(mzgh.getU_id());
        model.addAttribute("mId",mId);
        model.addAttribute("cno", cno);
        model.addAttribute("name", mzgh.getUname());
        model.addAttribute("dname", mzgh.getDname());
        LocalDateTime currentDateTime = LocalDateTime.now();
        model.addAttribute("zdrq", currentDateTime);
        return "doctor/addARecord";
   }
   @RequestMapping("/addARecord")
    public String addARecord(Record record, HttpSession session){
        dRecordService.addRecord(record);
         // Doctor currentDoctor = (Doctor) session.getAttribute("Doctor");
         //Integer doctorId = currentDoctor.getId();
        return "redirect:/doctor/showDRecordList?dId=1";//测试
   }
}
